﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aulac.Rsc.Bss.Tags;
using Aulac.Client.Global.Interface;
using System.Windows.Forms;
using Aulac.Rsc.Bss;
using Aulac.Global.Common;
using Aulac.UtilityTools;

namespace Aulac.Client.Bss.WinForm.CapAss
{
    public partial class FormDlgStaff
    {
        #region Override
        public override void OnFormLoad()
        {
            base.OnFormLoad();
            //cboCapAssGrp.AutoLoadData = true;
            this.AulacNavCtrl.ShowControlAll(false);
            this.AulacNavCtrl.ShowControl(true, AulacNavCtrl.DelControl, AulacNavCtrl.SaveControl, AulacNavCtrl.ExitControl);
            txtStaffID.Focus();
        }

        protected override void OnSetStateControls(Aulac.Global.Enums.FormModes pFormMode)
        {
            base.OnSetStateControls(pFormMode);
            this.AulacNavCtrl.ShowControlAll(false);
            if (pFormMode == Aulac.Global.Enums.FormModes.LockMode || pFormMode == Aulac.Global.Enums.FormModes.ViewMode)
            {
                List<IAulacDataControl> mLstControls = null;
                GetListAulacDataControls(ref mLstControls, grBoxInfo);
                GetListAulacDataControls(ref mLstControls, groupBox1);
                foreach (IAulacStateCtrl ctr in mLstControls)
                    ctr.Enabled = false;
                this.AulacNavCtrl.SetStateControl(pFormMode);
            }
            else
            {
                List<IAulacDataControl> mLstControls = null;
                GetListAulacDataControls(ref mLstControls, grBoxInfo);
                GetListAulacDataControls(ref mLstControls, groupBox1);
                foreach (IAulacStateCtrl ctr in mLstControls)
                    ctr.Enabled = true;
            }
            if (pFormMode == Aulac.Global.Enums.FormModes.NewMode)
            {
                chkIsActive.Checked = true;                
            }                        
            btnNav.ShowButton(true, btnNav.btnSave, btnNav.btnExit, btnNav.btnDel);
            btnNav.WidthButton = 100;
            this.AulacNavCtrl.SetStateControl(pFormMode);
        }
        protected override bool OnShowDetailVoucher()
        {
            //cboCapAssGrp.AutoLoadData = true;
            //cboCapAssGrp.ReLoadDataToControl();
            //cboCapAssType.AutoLoadData = true;
            //cboCapAssType.ReLoadDataToControl();
            //cboCapAssType.CapAssTypePrkID = "";
            return GetDetailItemStaff();
        }
        protected override void OnAddNewVoucher()
        {
            ClearValueToControls();
            base.OnAddNewVoucher();
            txtStaffID.Focus();
        }
        protected override void OnEditVoucher()
        {
            //cboCapAssGrp.AutoLoadData = true;
            //cboCapAssGrp.ReLoadDataToControl();
            //cboCapAssType.AutoLoadData = true;
            //cboCapAssType.ReLoadDataToControl();
            base.OnEditVoucher();
            GetDetailItemStaff();            
            txtStaffID.Focus();
        }
        protected override bool OnBeginSaveVoucher()
        {
            if (txtStaffID.IsEmpty)
            {
                MsgBox.Show("Mã nhân viên không được rỗng !", MsgBox.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtStaffID.Focus();
                return false;
            }
            if (txtStaffName.IsEmpty)
            {
                Aulac.UtilityTools.MsgBox.Show("Tên nhân viên không được rỗng !", "AulacSoft", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtStaffName.Focus();
                return false;
            }
            return base.OnBeginSaveVoucher();
        }        
        protected override bool OnSaveVoucherData(ref System.Xml.XmlDataDocument xmlData)
        {
            return SaveItemStaff(ref xmlData);
        }
        protected override void OnEndSaveVoucher(Aulac.Global.Enums.FormModes pEndFormMode, System.Xml.XmlDataDocument xmlData)
        {
            base.OnEndSaveVoucher(pEndFormMode, xmlData);
        }
        protected override bool OnDeleteDataVoucher()
        {
            return DeleteStaff();
        }
        protected override bool OnBeginExit()
        {
            if (FormMode == Aulac.Global.Enums.FormModes.EditMode || FormMode == Aulac.Global.Enums.FormModes.NewMode)
                return true;
            else
                FormMode = Aulac.Global.Enums.FormModes.ViewMode;
            return true;
        }
        #endregion

        #region Method
        private void SetFieldMember()
        {
            // ---------------- SET FieldMember ------------------
            txtStaffID.FieldMember = tagItems.Staff.StaffID;
            txtStaffName.FieldMember = tagItems.Staff.StaffName;
            txtDateDang.FieldMember = tagItems.Staff.DateInDang;
            txtDateStart.FieldMember = tagItems.Staff.DateStartWork;
            txtDateStop.FieldMember = tagItems.Staff.DateStopWork;
            txtNote.FieldMember = tagItems.Staff.Note;
            txtStaffFunction.FieldMember = tagItems.Staff.StaffFunction;
            txtStaffPlace.FieldMember = tagItems.Staff.StaffPlace;
            txtStaffPosition.FieldMember = tagItems.Staff.StaffPosition;
            txtStaffPhone.FieldMember = tagItems.Staff.StaffPhone;
            txtStaffAddr.FieldMember = tagItems.Staff.StaffAddr;
            txtStaffBirthday.FieldMember = tagItems.Staff.StaffBirthday;
            txtStaffEmail.FieldMember = tagItems.Staff.StaffEmail;
            comboCountry1.AddFieldMapping(tagItems.Country.CountryPrkID, tagItems.Staff.CountryPrkID);
            comboDept1.AddFieldMapping(tagItems.Department.DeptPrkID, tagItems.Staff.DeptPrkID);
            comboStaffType1.AddFieldMapping(tagItems.StaffType.StaffTypePrkID, tagItems.Staff.StaffTypePrkID);
            comboSexStaff1.AddFieldMapping(tagItems.SexStaff.SexID, tagItems.Staff.SexID);
            chkIsActive.FieldMember = tagItems.Staff.IsActive;
        }

        /// <summary>
        /// Xóa dữ liệu các Controls trong Form.
        /// </summary>
        public void ClearValueToControls()
        {
            List<IAulacDataControl> mListControl = null;
            GetListAulacDataControls(ref mListControl, grBoxInfo);
            GetListAulacDataControls(ref mListControl, groupBox1);
            foreach (IAulacValueDataCtrl ctr in mListControl)
            {
                ctr.ClearValueControls();
            }
        }

        private void GetListAulacDataControls(ref List<IAulacDataControl> mListControl, System.Windows.Forms.GroupBox grpBox)
        {
            if (mListControl == null)
                mListControl = new List<IAulacDataControl>();
            foreach (Control ctr in grpBox.Controls)
            {
                if (ctr is IAulacDataControl)
                    mListControl.Add((IAulacDataControl)ctr);
            }
        }
        #endregion        

        #region Properties
        private string mStaffID = "";
        public string StaffID
        {
            get
            { return mStaffID; }
            set
            { mStaffID = value; }
        }
        private string mStaffPrkID = "";
        public string StaffPrkID
        {
            get
            { return mStaffPrkID; }
            set
            { mStaffPrkID = value; }
        }
        private string mTitleName = "";
        public string TitleName 
        {
            get
            { return mTitleName;}
            set
            { 
                mTitleName = value;                
                if (!Aulac.Global.Common.StringHelper.IsEmpty(mTitleName))
                {
                    this.lblTitle.Text = mTitleName;
                    this.Text = Aulac.Global.Common.StringHelper.ReplaceIndex(mTitleName.ToLowerInvariant(), mTitleName.Substring(0, 1).ToLower(), mTitleName.Substring(0, 1).ToUpper(), 0);
                }
            } 
        }
        #endregion
    }
}
